(ns main.manager-view
  (:use 
    main.views
    main.db
    main.waiter-view
    ring.util.response
    [hiccup page-helpers form-helpers]
    sandbar.stateful-session))

(defn manager-menu-member [x]
  [:p [:a {:href (str "/manager/select/" (x 0))}(x 0)]])

(defn manager-menu-list []
  (map manager-menu-member (group-by :sub-category (get-articles {}))))

(defn manager-view [title & content]
  (try
  (if-not (.equals ((user-session) :role) "Manager")
    (redirect "/")
    (layout title
            [:table.left [:tr 
                            [:td [:div#navigation [:a "Articles"]
                                  [:div
                                   [:p [:a {:href "/manager/select/all"}"All Articles"]]
                                  (manager-menu-list)]]]
                            [:td [:div#navigation [:a "Waiters"]
                             [:div 
                              [:p [:a {:href "/manager/waiters"}"All waiters"]]
                              [:p [:a {:href "/manager/waiters"}"Current waiters"]]
                              [:p [:a {:href "/manager/waitersinshift"}"Waiters in shift"]]]]]
                            [:td [:div#navigation [:a "Reports"]
                                  [:div 
                                   [:p [:a {:href "/manager/allreports"}"All reports"]]
                                   [:p [:a {:href "/manager/todaysreports"}"Todays report"]]
                                   (waiter-menu-list)]
                                  ]]]]
            [:table.right [:tr [:td [:a {:href "/lock"}"Lock"]]]]
            [:div#clear]
            content
            [:div#clear]))
  (catch NullPointerException e (show-error "Access denied!"))))

(defn manager-view-all-waiters []
  (manager-view "Waiters"
          [:div.left(users-table {:role "Waiter"})]
          [:div {:style "float: right; border: solid green 1px; border-radius: 5px; padding: 10px;"}
           (form-to [:post "/manager/adduser"]
                    [:p [:label {:for "fname"} "First name"][:br] (text-field {:required "required"} :fname) ]
                    [:p [:label {:for "lname"} "Last name"][:br] (text-field {:required "required"} :lname) ]
                    [:p [:label {:for "passcode"} "Passcode"][:br] (text-field {:required "required"} :passcode) ]
                    [:p (submit-button {:id "button"} "Add waiter")])]
          ))

(defn manager-view-waiters-in-shift []
  (manager-view "Waiters in shift"
                (users-table {:inshift 1 :role "Waiter"})))

(defn manager-view-articles []
  (manager-view "Articles"
                (if (= (session-get :selected-category) nil)
                  (admin-articles {})
                  (admin-articles {:sub-category (session-get :selected-category)}))))

(defn manager-add-user [fname lname passcode]
  (if (== (user-count passcode) 0)
    (if (.equals (validate (required fname) (required lname) (required passcode)) "true")
      (do
        (save-user fname lname passcode "Waiter")
        (manager-view-all-waiters))
      (show-error (flash-get :error-message)))
    (show-error (str "Passcode " passcode " is taken, chosse another"))))

(defn manager-change-shift [id]
  (do
    (start-end-shift id)
    (redirect (str "/user/" id))))

(defn report-article-row [a]
  (let [x (get-article (a :article-id))]
    [:tr 
     [:td (x :label)]
     [:td (a :quantity)]]))

(defn manager-bill-view [x]
  (try
    (do
      [:div
       [:div.left
        [:p (str "Waiter: " (x :waiter-name))]
        [:p (str "Date: " (x :date))]
        [:p (str "Time: " (x :time))]
        [:p (str "Total: " (format "%.2f" (x :total)) " EUR")]]
       [:div.right 
        [:table.billTable
         [:tr 
          [:td.billCell "Label"]
          [:td.billCell "Quantity"]]
         (map report-article-row (x :articles))]]
       [:div#clear][:hr]])
    (catch NullPointerException e [:p "Error reading bill"])))

(defn manager-view-all-reports []
  (manager-view "All reports"
                (map manager-bill-view (reverse (get-reports {})))))

(defn manager-view-todays-reports []
  (manager-view "Today's reports"
                (map manager-bill-view (reverse (get-reports {:date (get-date) })))))

(defn manager-view-reports-of [id]
  (manager-view (str ((get-user id) :fname) " " ((get-user id) :lname) " reports")
                (map manager-bill-view (reverse (get-reports {:waiter-id id})))))

(defn set-category [cat]
  (if (.equals cat "all")
    (session-delete-key! :selected-category)
    (session-put! :selected-category cat))
  (redirect "/manager"))